/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Data;

using DotNetNuke.ModuleToolkit.Common;

#endregion;

namespace DotNetNuke.ModuleToolkit.Modules.Data
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The DataService Class provides Data Layer Utilities for the Project
    /// </summary>
    /// <history>
    /// 	[cnurse]	10/12/2007	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class DataService
    {

        #region Private Static Members

        private static DataProvider provider = DataProvider.Instance();
        private static string moduleQualifier = "Toolkit_";

        #endregion

        #region Private Static Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value of the field, or if Null returns DbNull
        /// </summary>
        /// <param name="Field">The field to evaluate</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static Object GetNull(Object Field)
        {
            return Null.GetNull(Field, DBNull.Value);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the fully qualified name of a stored procedure
        /// </summary>
        /// <param name="name">The name of the Stored procedure</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static string GetFullyQualifiedName(string name)
        {
            return moduleQualifier + name;
        }

        #endregion

        #region Public Static Methods

        #region Extension Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a Extension
        /// </summary>
        /// <param name="packageId">The id of the package</param>
        /// <param name="desktopModuleId">The id of the module</param>
        /// <param name="name">The name of the Extension</param>
        /// <param name="IExtensionClass">The class that implements the IExtension Interface</param>
        /// <returns>The Id of the new Extension</returns>
        /// <history>
        /// 	[cnurse]	10/13/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static int CreateExtension(int packageId, int desktopModuleId, string name, string IExtensionClass)
        {
            return provider.ExecuteScalar<int>(GetFullyQualifiedName("CreateExtension"), packageId, desktopModuleId, "Extensions", name, IExtensionClass);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes a Extension
        /// </summary>
        /// <param name="extensionId">The id of the Extension</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void DeleteExtension(int extensionId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("DeleteExtension"), extensionId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a Extension from the Data Store
        /// </summary>
        /// <param name="extensionId">The id of the Extension</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetExtension(int extensionId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetExtension"), extensionId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a Extension from the Data Store
        /// </summary>
        /// <param name="desktopModuleId">The id of the module</param>
        /// <param name="extensionName">The name of the Extension</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetExtensionByName(int desktopModuleId, string extensionName)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetExtensionByName"), desktopModuleId, extensionName);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a Extension from the Data Store by its Package Id
        /// </summary>
        /// <param name="packageId">The id of the package</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetExtensionByPackageID(int packageId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetExtensionByPackageID"), packageId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a collection of Registered Extensions from the Data Store
        /// </summary>
        /// <param name="desktopModuleId">The id of the module</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetExtensions(int desktopModuleId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetExtensions"), desktopModuleId);
        }

        #endregion

        #region Skin Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a Skin
        /// </summary>
        /// <param name="packageId">The id of the package</param>
        /// <param name="desktopModuleId">The id of the module</param>
        /// <param name="name">The name of the Skin</param>
        /// <returns>The Id of the new Skin</returns>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static int CreateSkin(int packageId, int desktopModuleId, string name)
        {
            return provider.ExecuteScalar<int>(GetFullyQualifiedName("CreateSkin"), packageId, desktopModuleId, "Skins", name);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes a Skin
        /// </summary>
        /// <param name="skinId">The id of the Skin</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void DeleteSkin(int skinId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("DeleteSkin"), skinId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a Skin from the Data Store
        /// </summary>
        /// <param name="skinId">The id of the skin</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetSkin(int skinId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetSkin"), skinId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a Skin from the Data Store
        /// </summary>
        /// <param name="desktopModuleId">The id of the module</param>
        /// <param name="skinName">The name of the Skin</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetSkinByName(int desktopModuleId, string skinName)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetSkinByName"), desktopModuleId, skinName);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a Skin from the Data Store by its Package Id
        /// </summary>
        /// <param name="packageId">The id of the package</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetSkinByPackageID(int packageId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetSkinByPackageID"), packageId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a collection of Registered Skins from the Data Store
        /// </summary>
        /// <param name="desktopModuleId">The id of the module</param>
        /// <history>
        /// 	[cnurse]	10/12/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetSkins(int desktopModuleId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetSkins"), desktopModuleId);
        }

        #endregion

        #endregion

    }
}
